<?php

class navController extends Controller {
    public $initphp_list = array(
        'edit',
        'save',
        'del',
    );
    /**
     * 
     * @var navService
     */
    private $navService;
    public function __construct() {
        parent::__construct();
        $this->checkLogin();
        $this->navService=InitPHP::getService("nav");
    }
    public function run() {
        $data = $this->navService->getNav();
        $this->view->assign("menu_now", "nav_list");
        $this->view->assign("data", $data);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/nav/list");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "导航管理-凯巴斯-管理后台");
        $this->view->display();
    }
    public function edit(){
        $id=$this->controller->get_gp("id");
        if ($id>0){
            $data=$this->navService->getOne($id);
            $pid=$data['pid'];
            $this->view->assign("id", $id);
            $this->view->assign("data", $data);
        }else {
            $pid=$this->controller->get_gp("pid");
        }
        $parent=$this->navService->getOne($pid);
        $this->view->assign("menu_now", "nav_list");
        $this->view->assign("parent", $parent);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/nav/add");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "添加导航-凯巴斯-管理后台");
        $this->view->display();
    }
    public function del(){
        $id=$this->controller->get_gp("id");
        $this->navService->delete($id);
        JsMessage("删除成功！","/manage/nav/run");
    }
    public function save(){
        $id=$this->controller->get_gp("id");
        $data=$this->controller->get_gp(array(
            'title',
            'sort',
            'url',
            'target',
            'status',
            'pid',
        ));
        if (!$data['pid']){
            $data['pid']=0;
        }
        $data['update_time']=time();
        if ($id){
            $this->navService->update($id, $data);
        }else {
            $data['add_time']=time();
            $this->navService->add($data);
        }
        JsMessage("保存成功！",'/manage/nav/run');
    }
    /**
     * 检查登陆状态
     */
    public function checkLogin() {
        $session = $this->getUtil('session');
        $adminid = $session->get('adminid');
        if (empty($adminid)) {
            $function = $this->getLibrary('function');
            $function->goto_url('/manage/login');
            die();
        }
        $this->view->assign("adminname", $session->get('adminname'));
        $this->view->assign("adminid", $session->get('adminid'));
    }
}